package com.qiang.test05;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import static java.lang.Class.*;

public class Demo02 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Method01();
        //Method02();
        //Method03();
        //Method04();

    }

    public static void Method04() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //创建类
        //Student st = new Student();
        Class st = Student.class;
        Constructor dd = st.getDeclaredConstructor(String.class);
        dd.setAccessible(true);
        Student stu = (Student)dd.newInstance("张三");
        System.out.println(stu);
    }

    public static void Method03() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //创建类
        Class sc = Student.class;

        //获取私有化的无参构造
        Constructor dd = sc.getDeclaredConstructor();
        Student student = (Student) dd.newInstance();
        System.out.println(student);
    }

    public static void Method02() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //创建类
        Class sc = forName("com.qiang.test05.Student");

        //获取公共的无参构造对象
        Constructor sd = sc.getConstructor();
        Student stu = (Student)sd.newInstance();
        System.out.println(stu);
    }

    public static void Method01() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //创建类
        Class sc = forName("com.qiang.test05.Student");
        //获取公共的有参构造对象
        Constructor con = sc.getConstructor(String.class,int.class);
        Student stu = (Student)con.newInstance("张三", 23);
        System.out.println(stu.getClass());
    }
}
